#!/usr/bin/python
# -*- coding: UTF-8 -*-
ip="172.21.39.1"

import time
import sys
import json
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from kazoo.client import KazooClient


from controller_src import ps_moniter_controller
from controller_src.ttypes import *
from controller_src.constants import *


transport = TSocket.TSocket(ip, 27998)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ps_moniter_controller.Client(protocol)
# Connect!
transport.open()

if len(sys.argv) == 2 and sys.argv[1]=="info":
    ips = client.getInfo('{"cmd":"list"}')
    print(ips)
    msg = client.getInfo('{"cmd":"info", "ip":"172.21.39.2"}')
    print(msg)
    msg = client.getInfo('{"cmd":"info"}')
    print(msg)
    msg = client.getInfo('{"cmd":"model"}')
    print(msg)
    msg = client.getInfo('{"cmd":"model", "name":"for", "id":"test", "machine_type":"offline"}')
    print(msg)

if len(sys.argv) == 2 and sys.argv[1]=="mv":
    msg = client.runRealtimePs('{"model_name":"for","model_id":"test"}')
    print(msg) # 后续要保存
    exit()


cmd=""
if len(sys.argv) == 4:
    cmd = '{"model_name":"%s","model_id":"%s"}' % (sys.argv[1], sys.argv[2])
if len(sys.argv) == 5:
    cmd = '{"model_name":"%s","model_id":"%s", "machine_type":"%s"}' % (sys.argv[1], sys.argv[2], sys.argv[4])
if len(sys.argv) == 1:
    msg = client.runOfflinePs('{"model_name":"for","model_id":"test"}')
    print(msg) # 后续要保存
    time.sleep(3)
    msg = client.stopPs('{"model_name":"for","model_id":"test"}')
    print(msg)

elif len(sys.argv) >= 4 and sys.argv[3] == "run":
    msg = client.runOfflinePs(cmd)
    print(msg) # 后续要保存
elif len(sys.argv) >= 4 and sys.argv[3] == "stop":
    msg = client.stopPs(cmd)
    print(msg) # 后续要保存









transport.close() 


